<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item
            label="名称"
            prop="name"
            :rules="[{ required: true, message: '请输入名称', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="当前状态"
            prop="sfqy"
            :rules="{ required: true, message: '请选择当前状态', trigger: 'change' }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.sfqy" style="width: 200px">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="排序"
            prop="px"
            :rules="[{ required: true, message: '请输入排序值', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input-number v-model="form.px" />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateObject, saveObject } from '@/api/portal/manager/child/index';
export default {
  name: 'PortalTempParentTypeEntity',
  data() {
    return {
      title: '大类管理',
      loading: false,
      dialogFormVisible: false,
      status: [{ id: 0, value: false, label: '禁用' },
        { id: 1, value: true, label: '启用' }],
      form: {
        id: null,
        name: '',
        px: 1,
        sfqy: true,
        parent: null
      }
    };
  },
  methods: {
    addEntity(data) {
      this.form.parent = data;
      this.title = '新增';
      this.dialogFormVisible = true;
    },
    openEntity(data) {
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = Object.assign({}, data);
        this.title = '修改信息';
        this.dialogFormVisible = true;
      } else {
        this.title = '新增';
        this.dialogFormVisible = true;
      }
    },
    save(formName) {
      this.loading = true;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！');
              this.close();
            }).catch(() => {
              this.loading = false;
              this.$message.error('更新失败！');
            });
          } else {
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！');
              this.close();
              this.loading = false;
            }).catch(() => {
              this.loading = false;
              this.$message.error('保存失败！');
            });
          }
        } else {
          this.loading = false;
          console.log('error submit!!');
          return false;
        }
      });
    },
    close() {
      this.loading = false;
      Object.assign(this.$data.form, this.$options.data.call(this).form);
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    }
  }
};
</script>

<style scoped>

</style>
